package app.netant.finishbook.jjxsw;

import org.jsoup.select.Elements;

import app.netant.NetAntBase;

public class JJxswDetail extends NetAntBase {
	
	private JJxswEntity entity;
	
	private String exp = "";

	public JJxswDetail(String netSite) {
		super(netSite);
	}
	
	public void setEntity(JJxswEntity entity){
		this.entity = entity;
	}
	
	public void analyseType(){
		
		Elements activePart = document.select("#wrap #txt99 #main_down #path a");
		if(activePart.size()>1){
			String type = activePart.get(1).text();
			System.out.println("【Type】:"+type);
			this.entity.setNovelType(type);
		}
		
	}

	public void analyseTitle(){
		
		Elements activePart = document.select("#wrap #txt99 #main_down #pageMainArea #downInfoArea h1");
		if(activePart.size()>0){
			String title = activePart.get(0).text();
			System.out.println("【Title】:"+title);
			this.entity.setNovelName(title);
		}
		
	}
	
	public void analyseTags(){
		
		Elements activePart = document.select("#wrap #txt99 #main_down #pageMainArea #downInfoArea .downInfoRowL li");
		if(activePart.size()>0){
			for(int i=0;i<activePart.size();i++){
				String tags = activePart.get(i).text();
				int idx = tags.indexOf("：");
				if(idx > 0){
					String tagName = tags.substring(0,idx);
					String tagValue = tags.substring(idx+1).replaceAll(" ", "");
					System.out.println("【TAG】"+tagName+"="+tagValue);
					this.entity.setTag(tagName, tagValue);
				}
			}
		}
		
	}
	
	public void analyseMemo(){
		
		Elements activePart = document.select("#wrap #txt99 #main_down #mainstory #mainSoftIntro");
		if(activePart.size()>0){
			String memo = activePart.get(0).text();
			System.out.println("【Memo】:"+memo);
			this.entity.setNovelMemo(memo);
		}
		
	}
	
	public void analyseHref(){
		
		Elements activePart = document.select("#wrap #txt99 #main_down #mainstory ul.downlistbox li.downAddress_li a");
		if(activePart.size()>0){
			String href = activePart.get(0).attr("href");
			if(href.startsWith("/")){
				href = "http://www.jjxsw.com" + href;
			}
			
			System.out.println("开始解析链接地址.........");
			JJxswDetail detail = new JJxswDetail(href);
			
			String url = detail.getHref();
			this.exp = detail.exp;
			
			System.out.println("【Href】:"+url);
			
			this.entity.setDownPath(url);
			
			String name = this.entity.getNovelName();
			
			this.entity.setNovelFullName(name+this.exp);
			
		}
		
	}
	
	public String getHref(){
		
		Elements activePart = document.select("#wrap #txt99 #Frame a.strong.green");
		if(activePart.size()>0){
			String href = activePart.get(0).attr("href");
			String txt = activePart.get(0).text().trim();
			
			if(txt.indexOf("TXT")>-1){
				this.exp = ".txt";
			}else if(txt.indexOf("RAR")>-1){
				this.exp = ".rar";
			}
			
			System.out.println("【EXP】:"+this.exp);
			
			if(href.startsWith("/")){
				href = "http://m.jjxsw.com" + href;
			}
			return href;
		}else{
			throw new RuntimeException("无法获取准确下载地址.........");
		}
		
		
	}
	
	public void analyse(){
		
		this.analyseType();
		
		if(this.entity.getNovelName() == null ||"".equals(this.entity.getNovelName())){
			this.analyseTitle();
		}
		
		this.analyseTags();
		this.analyseMemo();
		this.analyseHref();
		
		this.entity.download();
		this.entity.insert();
		
	}

	public static void main(String[] args) {
		
		JJxswDetail detail = new JJxswDetail("http://www.jjxsw.com/txt/29407.htm");
		
		/*detail.analyseType();
		detail.analyseTitle();
		detail.analyseTags();
		detail.analyseMemo();*/
		detail.analyseHref();
		
	}
	
}
